/////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2000-2011 OpenNETCF Consulting, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OpenNETCF.Windows.Forms.Calendar
{
    internal partial class AgendaList : OwnerDrawnList
    {
        public new event DrawItemEventHandler DrawItem;
        private bool NeedToFireOnSelChanged;
        public AgendaList()
            : base()
        {
            InitializeComponent();
            NeedToFireOnSelChanged = false;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
        }
        protected override void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            if (DrawItem != null)
            {
                DrawItem(sender, e);
                return;
            }

            Font m_fontBold = new Font(Font.Name, Font.Size, FontStyle.Bold);
            Brush m_brDayFG;
            Pen m_penGrid = new Pen(SystemColors.WindowFrame);
            Rectangle rcClip = e.Bounds;
            rcClip.Inflate(-1, -1);
            e.Graphics.Clip = new Region(rcClip);
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && e.Index < (DataSource as CalendarItem[]).Length)
            {
                e.Graphics.Clear(SystemColors.Highlight);
                m_brDayFG = new SolidBrush(SystemColors.HighlightText);
            }
            else
            {
                e.Graphics.DrawImage(BackgroundImage, e.Bounds, e.Bounds, GraphicsUnit.Pixel);
                m_brDayFG = new SolidBrush(SystemColors.ControlText);
            }
            if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
                e.DrawFocusRectangle();
            CalendarItem item = (DataSource as CalendarItem[])[e.Index];
            RectangleF rcf = new RectangleF(e.Bounds.Left + 2, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height / 2);
            e.Graphics.DrawString(item.Text, m_fontBold, m_brDayFG, rcf);
            string sRange = string.Format("{0}-{1}", item.StartTime.ToString("hh:MMt"), item.EndTime.ToString("hh:MMt"));
            rcf = new RectangleF(rcf.Left, rcf.Bottom, rcf.Width, rcf.Height);
            e.Graphics.DrawString(sRange, Font, m_brDayFG, rcf);
            e.Graphics.DrawLine(m_penGrid, e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Top);
            m_brDayFG.Dispose();
            e.Graphics.ResetClip();
        }

        protected override object BuildItemForRow(object row)
        {
            return BaseItems.Add(row.ToString());
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            NeedToFireOnSelChanged = true;
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            NeedToFireOnSelChanged = false;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (NeedToFireOnSelChanged)
            {
                NeedToFireOnSelChanged = false;
                OnSelectedIndexChanged(EventArgs.Empty);
            }
        }
    }
}
